package com.itic.appbase.framework.common.service;

import java.util.Date;

import org.slf4j.Logger;

import com.itic.appbase.framework.common.constants.Constants;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.persistence.model.BaseModel;
import com.itic.appbase.framework.plugins.logger.Log;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.UserSessionHelper;
import com.itic.appbase.framework.utils.StringHelper;

/**
 * SERVICE基类，提供SERVICE需要的公共方法.
 * 
 * @ClassName: BaseService
 * @author: djp_ivan
 * @date: 2015年4月14日 下午3:00:38
 * @version: V1.0
 * @Update_Comments: by djp_ivan at 2015年4月14日 - description
 */
public class BaseService {
    /**
     * 日志对象
     */
    protected Logger LOGGER = Log.get(getClass());

    /**
     * 默认值设置
     * 
     * @param model
     * @return
     */
    protected <M extends BaseModel> M initDefalutFields(M model) {

        if (null == model.getId() || StringHelper.isBlank(model.getId())) {
            model.setId(IdGen.uuid());
        }
        if (null == model.getCreateTime()) {
            model.setCreateTime(new Date());
        }
        if (null == model.getUpdateTime()) {
            model.setUpdateTime(new Date());
        }
        if (null == model.getCreateUser() || StringHelper.isBlank(model.getCreateUser())) {
            model.setCreateUser(null != UserSessionHelper.get(Constants.USER_ID) ? UserSessionHelper
                    .get(Constants.USER_ID) : "unknown");
        }
        if (null == model.getOrgCode() || StringHelper.isBlank(model.getOrgCode())) {
            model.setOrgCode(null != UserSessionHelper.get(Constants.USER_ORG) ? UserSessionHelper
                    .get(Constants.USER_ORG) : "unknown");
        }
        if (null == model.getIsEffective() || StringHelper.isBlank(model.getIsEffective())) {
            model.setIsEffective(DBConst.TRUE);
        }
        return model;
    }
}
